<?php

namespace App\Observers;

use App\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Redis;

class UserObserver
{

    public function updated(User $user)
    {
        if ($user->status != $user->getOriginal('status') && in_array($user->status, [1, 0])) {
            // A change to the price has occurred.
            $role = $user->getRoleNames()[0];
            if (!$role || $role === 'admin') {
                return;
            }
            $online = getOnlineUser($role);

            $in_array = in_array($user->id, $online);

            if ($user->status && !$in_array) {
                array_push($online, $user->id);
            } else if (!$user->status && $in_array) {
                $online = array_diff($online, [$user->id]);
            }
            setOnlineUser($role, $online);
        }
    }
}
